package com.brainhealth.customer.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.brainhealth.common.enums.CustomerExerciseStatus;
import com.brainhealth.common.enums.CustomerScoreType;
import com.brainhealth.common.enums.PayStatus;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.customer.domain.CustomerExercise;
import com.brainhealth.customer.domain.ExerciseOrder;
import com.brainhealth.customer.mapper.CustomerExerciseMapper;
import com.brainhealth.customer.mapper.ExerciseOrderMapper;
import com.brainhealth.utils.CommonUtils;
import com.fasterxml.jackson.databind.ser.std.StdArraySerializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.CustomerScoreMapper;
import com.brainhealth.customer.domain.CustomerScore;
import com.brainhealth.customer.service.ICustomerScoreService;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员质量评分Service业务层处理
 * 
 * @author wolong
 * @date 2023-05-17
 */
@Service
public class CustomerScoreServiceImpl implements ICustomerScoreService 
{
    @Autowired
    private CustomerScoreMapper customerScoreMapper;
    @Autowired
    private CustomerExerciseMapper customerExerciseMapper;
    @Autowired
    private ExerciseOrderMapper exerciseOrderMapper;

    /**
     * 查询会员质量评分
     * 
     * @param customerScoreId 会员质量评分主键
     * @return 会员质量评分
     */
    @Override
    public CustomerScore selectCustomerScoreByCustomerScoreId(Long customerScoreId)
    {
        return customerScoreMapper.selectCustomerScoreByCustomerScoreId(customerScoreId);
    }

    /**
     * 查询会员质量评分列表
     * 
     * @param customerScore 会员质量评分
     * @return 会员质量评分
     */
    @Override
    public List<CustomerScore> selectCustomerScoreList(CustomerScore customerScore)
    {
        return customerScoreMapper.selectCustomerScoreList(customerScore);
    }

    /**
     * 新增会员质量评分
     * 
     * @param customerScore 会员质量评分
     * @return 结果
     */
    @Override
    public int insertCustomerScore(CustomerScore customerScore)
    {
        return customerScoreMapper.insertCustomerScore(customerScore);
    }

    /**
     * 修改会员质量评分
     * 
     * @param customerScore 会员质量评分
     * @return 结果
     */
    @Override
    public int updateCustomerScore(CustomerScore customerScore)
    {
        return customerScoreMapper.updateCustomerScore(customerScore);
    }

    /**
     * 批量删除会员质量评分
     * 
     * @param customerScoreIds 需要删除的会员质量评分主键
     * @return 结果
     */
    @Override
    public int deleteCustomerScoreByCustomerScoreIds(Long[] customerScoreIds)
    {
        return customerScoreMapper.deleteCustomerScoreByCustomerScoreIds(customerScoreIds);
    }

    /**
     * 删除会员质量评分信息
     * 
     * @param customerScoreId 会员质量评分主键
     * @return 结果
     */
    @Override
    public int deleteCustomerScoreByCustomerScoreId(Long customerScoreId)
    {
        return customerScoreMapper.deleteCustomerScoreByCustomerScoreId(customerScoreId);
    }

    /**
     * 根据会员学习训练来添加质量评分
     * @param request
     * @param learnId
     * @return
     */
    @Override
    public boolean setCustomerScore(HttpServletRequest request, Long learnId)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        //如果是体验用户不添加质量评分
        if (customer.getMechanismId() != null && customer.getMechanismId().equals(-1L))
        {
            return true;
        }
        ExerciseOrder queryOrder = new ExerciseOrder();
        queryOrder.setStatus(PayStatus.Exercise);
        List<ExerciseOrder> lstOrder = exerciseOrderMapper.selectExerciseOrderList(queryOrder);
        ExerciseOrder order = new ExerciseOrder();
        if (lstOrder.size() > 0)
        {
            order = lstOrder.get(0);
        }
        CustomerExercise query = new CustomerExercise();
        query.setCustomerId(customer.getCustomerId());
        query.setOrderId(order.getOrderId());
        query.setStatus(CustomerExerciseStatus.NoFinish);
        List<CustomerExercise> lstCustomerExercise = customerExerciseMapper.selectCustomerExerciseList(query);
        if (lstCustomerExercise.size() > 0)
        {
            CustomerExercise customerExercise = lstCustomerExercise.get(0);
            CustomerScore queryScore = new CustomerScore();
            queryScore.setCustomerId(customer.getCustomerId());
            queryScore.setOrderId(customerExercise.getOrderId());
            List<CustomerScore> lstScore = customerScoreMapper.selectCustomerScoreList(queryScore);
            int listSize = order.getCycle().intValue() / 7;
            List<List<CustomerScore>> lstGroupData = CommonUtils.getGroupList(listSize, lstScore);
            Long pos = customerExercise.getCountSort() / 7;
            CustomerScore customerScore;
            if (pos == lstGroupData.size())
            {
                customerScore = lstGroupData.get(lstGroupData.size() - 1).stream().filter(item -> item.getScoreType() == CustomerScoreType.Learn).findFirst().orElse(null);
            } else
            {
                customerScore = lstGroupData.get(pos.intValue()).stream().filter(item -> item.getScoreType() == CustomerScoreType.Learn).findFirst().orElse(null);
            }
            if (customerScore != null)
            {
                return false;
            } else
            {
                customerScore = new CustomerScore();
                customerScore.setOrderId(customerExercise.getOrderId());
                customerScore.setCustomerId(customer.getCustomerId());
                customerScore.setScoreType(CustomerScoreType.Learn);
                customerScore.setCountSort(customerExercise.getCountSort());
                customerScore.setScore(30l);
                customerScore.setTypeId(learnId);
                customerScoreMapper.insertCustomerScore(customerScore);
            }
            return true;
        }
        return false;
    }
}
